<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>在 JavaScript 行内创建工作者线程</title>
</head>
<body>
  <script>
    //  创建要执行的 JavaScript 代码字符串
    const workerScript = 'self.onmessage = ({ data }) => console.log(data);';

    // 基于脚本字符串生成 Blob 对象
    const workerScriptBlob = new Blob([workerScript]);

    // 基于 Blob 实例创建对象 URL
    const workerScriptBlobUrl = URL.createObjectURL(workerScriptBlob);

    // 基于对象 URL 创建专用工作者线程
    const worker = new Worker(workerScriptBlobUrl);

    worker.postMessage('blob worker script');
    // blob worker script
  </script>
</body>
</html>